package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.LogisticBean;
import com.realbrand.net.realbrandpay.bean.LogisticTwoBean;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.weight.MyListView;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/12.
 */

public class MyLogisticAdapter extends BaseAdapter {
    List<LogisticBean> data;
    Context mContext;
    LayoutInflater inflater;

    public MyLogisticAdapter(List<LogisticBean> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_logistic, parent, false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.tv_day.setText(data.get(position).getTime().split(" ")[0]);
        myViewHolder.tv_hour.setText(data.get(position).getTime().split(" ")[1]);
        myViewHolder.tv_name.setText(data.get(position).getWorkAddress());
        myViewHolder.tv_address.setText(data.get(position).getEnterpriseName());
        if (position == data.size()-1) {
            myViewHolder.tv_print.setVisibility(View.VISIBLE);
        } else {
            myViewHolder.tv_print.setVisibility(View.GONE);
        }
        if (data.get(position).getItem_type() == 1) {
            myViewHolder.iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrival_g));
            myViewHolder.tv_logistic.setVisibility(View.GONE);
            myViewHolder.lv_traces.setVisibility(View.GONE);
        } else if (data.get(position).getItem_type() == 2) {
            myViewHolder.iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_outposition_g));
            myViewHolder.tv_logistic.setVisibility(View.VISIBLE);
            myViewHolder.lv_traces.setVisibility(View.VISIBLE);
            myViewHolder.tv_logistic.setText(data.get(position).getLogisticsName() + ":" + data.get(position).getOddNumbers());
            String traces = data.get(position).getTraces();
            if (!traces.equals("0")) {
                String substring = traces.substring(2, traces.length() - 2);
                String[] split = substring.split("\"\\},\\{\"");
                L.d("数组" + split.toString());
                List<JSONObject> list = new ArrayList<>();
                List<LogisticTwoBean> twoBeanList = new ArrayList<>();
                for (int i = 0; i < split.length; i++) {
                    try {
                        String s = split[i];
                        s.replace(" ", "");
                        L.d("字符串" + s);
                        JSONObject object = new JSONObject("{\"" + s + "\"}");
                        list.add(object);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        L.d("json异常" + e.toString());
                    }
                }
                for (int n = 0; n < list.size(); n++) {
                    try {
                        String acceptStation = list.get(n).getString("AcceptStation");
                        String acceptTime = list.get(n).getString("AcceptTime");
                        LogisticTwoBean twoBean = new LogisticTwoBean();
                        twoBean.setAcceptTime(acceptTime);
                        twoBean.setAcceptStation(acceptStation);
                        twoBeanList.add(twoBean);
                    } catch (JSONException e) {
                        e.printStackTrace();
                        L.d("jsonTwo异常" + e.toString());
                    }
                }
                L.d("字符串List" + twoBeanList.toString() + "数组大小" + twoBeanList.size());
                LogisticTwoAdapter twoAdapter = new LogisticTwoAdapter(twoBeanList, mContext);
                myViewHolder.lv_traces.setAdapter(twoAdapter);
//                setListViewHeightBasedOnChildren(myViewHolder.lv_traces);
            }
        }
        return convertView;
    }

    class MyViewHolder {
        TextView tv_day;
        TextView tv_hour;
        TextView tv_name;
        TextView tv_address;
        TextView tv_logistic;
        MyListView lv_traces;
        ImageView iv;
        TextView tv_print;

        public MyViewHolder(View view) {
            tv_day = (TextView) view.findViewById(R.id.item_logistic_tv_day);
            tv_hour = (TextView) view.findViewById(R.id.item_logistic_tv_hour);
            tv_name = (TextView) view.findViewById(R.id.item_logistic_tv_Name);
            tv_address = (TextView) view.findViewById(R.id.item_logistic_tv_Address);
            tv_logistic = (TextView) view.findViewById(R.id.item_logistic_tv_Logistic);
            lv_traces = (MyListView) view.findViewById(R.id.item_logistic_tv_ListView);
            iv = (ImageView) view.findViewById(R.id.item_logistic_iv);
            tv_print = (TextView) view.findViewById(R.id.item_logistic_print);
        }
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
// 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() *
                (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
